package com.hksj.lntm.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.cbm.dto.AlarmPageDTO;
import com.hksj.cbm.entity.SpecialityModel;
import com.hksj.cbm.service.AlarmService;
import com.hksj.cbm.service.SpecialityModelService;
import com.hksj.common.core.common.Constants;
import com.hksj.common.core.common.DevicePropertyConstants;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import com.hksj.common.core.util.DateUtil;
import com.hksj.common.core.util.EasyExcelUtil;
import com.hksj.lntm.dto.LiquidNitrogenAlarmDetailDTO;
import com.hksj.lntm.mapper.LiquidNitrogenAlarmMapper;
import com.hksj.lntm.vo.DepthAlarmDetailExcelVO;
import com.hksj.lntm.vo.LiquidNitrogenAlarmDeviceInfoVO;
import com.hksj.lntm.vo.TemperatureAlarmDetailExcelVO;
import com.hksj.cbm.vo.AlarmNumRankingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fjt
 * @data 2024/11/11 21:00
 * @describe
 */
@Service
public class LiquidNitrogenAlarmService {


    @Autowired
    private LiquidNitrogenAlarmMapper liquidNitrogenAlarmMapper;
    @Autowired
    private SpecialityModelService specialityModelService;
    @Autowired
    private AlarmService alarmService;


    /**
     * 分页查询报警列表
     * @param dto
     * @return
     */
    public Page<LiquidNitrogenAlarmDeviceInfoVO> pageAlarm(LiquidNitrogenAlarmDetailDTO dto){
        dto.setCode(SpecialityModelCodeEnum.liquid_nitrogen.getCode());
        if (StringUtils.isNotBlank(dto.getStartTimeStr())){
            dto.setStartTime(DateUtil.getYmdHms(dto.getStartTimeStr() + " 00:00:00"));
        }
        if (StringUtils.isNotBlank(dto.getEndTimeStr())){
            dto.setEndTime(DateUtil.getYmdHms(dto.getEndTimeStr() + " 23:59:59"));
        }
        Page<LiquidNitrogenAlarmDeviceInfoVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<LiquidNitrogenAlarmDeviceInfoVO> list = liquidNitrogenAlarmMapper.queryAlarmDetail(dto, page);
        page.setRecords(list);
        return page;
    }


    /**
     * 导出报警记录
     * @param response
     * @param dto
     */
    public void exportAlarmList(HttpServletResponse response, LiquidNitrogenAlarmDetailDTO dto){
        dto.setCode(SpecialityModelCodeEnum.liquid_nitrogen.getCode());
        if (StringUtils.isNotBlank(dto.getStartTimeStr())){
            dto.setStartTime(DateUtil.getYmdHms(dto.getStartTimeStr() + " 00:00:00"));
        }
        if (StringUtils.isNotBlank(dto.getEndTimeStr())){
            dto.setEndTime(DateUtil.getYmdHms(dto.getEndTimeStr() + " 23:59:59"));
        }
        List<LiquidNitrogenAlarmDeviceInfoVO> list = liquidNitrogenAlarmMapper.queryAlarmDetail(dto, null);

        LambdaQueryWrapper<SpecialityModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SpecialityModel::getCode, SpecialityModelCodeEnum.air_conditioning.getCode());
        SpecialityModel specialityModel = specialityModelService.getOne(queryWrapper);
        if (ObjectUtil.isNull(specialityModel)){
            throw new RuntimeException("氨气监测系统缺少专业模型配置信息");
        }
        // 统计方式(1=按楼层统计(此时没有科室) 2=按科室统计)
        Integer statisticalMethod = specialityModel.getStatisticalMethod();

        // 液氮罐瓶内温度
        if (DevicePropertyConstants.liquid_nitrogen_temperature.equals(dto.getProperty())){
            List<TemperatureAlarmDetailExcelVO> dataList = new ArrayList<>();
            if (CollUtil.isNotEmpty(list)){
                if (Constants.ONE.equals(statisticalMethod)){
                    list.forEach(alarm -> {
                        // 楼栋->楼层->房间
                        StringBuilder deviceLocation = new StringBuilder(alarm.getBuildingName());
                        if (StringUtils.isNotBlank(alarm.getFloorName())){
                            deviceLocation.append("->").append(alarm.getFloorName());
                        }
                        if (StringUtils.isNotBlank(alarm.getRoomName())){
                            deviceLocation.append("->").append(alarm.getRoomName());
                        }
                        TemperatureAlarmDetailExcelVO excelVO = TemperatureAlarmDetailExcelVO.builder().deviceLocation(deviceLocation.toString())
                                .deviceName(alarm.getDeviceName()).value(alarm.getValue()).createTime(alarm.getCreateTime()).build();
                        dataList.add(excelVO);
                    });

                }
                // 2=按科室统计
                if (Constants.TWO.equals(statisticalMethod)){
                    list.forEach(alarm -> {
                        // 楼栋->楼层->科室->房间
                        StringBuilder deviceLocation = new StringBuilder(alarm.getBuildingName());
                        if (StringUtils.isNotBlank(alarm.getFloorName())){
                            deviceLocation.append("->").append(alarm.getFloorName());
                        }
                        if (StringUtils.isNotBlank(alarm.getDepartmentItemValueName())){
                            deviceLocation.append("->").append(alarm.getDepartmentItemValueName());
                        }
                        if (StringUtils.isNotBlank(alarm.getRoomName())){
                            deviceLocation.append("->").append(alarm.getRoomName());
                        }
                        TemperatureAlarmDetailExcelVO excelVO = TemperatureAlarmDetailExcelVO.builder().deviceLocation(deviceLocation.toString())
                                .deviceName(alarm.getDeviceName()).value(alarm.getValue()).createTime(alarm.getCreateTime()).build();
                        dataList.add(excelVO);
                    });
                }
            }
            String fileName = new StringBuffer().append("液氮罐瓶内温度—报警记录").append(DateUtil.getYmdHms()).append(".xlsx").toString();
            EasyExcelUtil.exportExcelData(response, fileName, dataList, TemperatureAlarmDetailExcelVO.class);
        }

        // 液氮罐液位深度
        if (DevicePropertyConstants.liquid_nitrogen_depth.equals(dto.getProperty())){
            List<DepthAlarmDetailExcelVO> dataList = new ArrayList<>();
            if (CollUtil.isNotEmpty(list)){
                if (Constants.ONE.equals(statisticalMethod)){
                    list.forEach(alarm -> {
                        // 楼栋->楼层->房间
                        StringBuilder deviceLocation = new StringBuilder(alarm.getBuildingName());
                        if (StringUtils.isNotBlank(alarm.getFloorName())){
                            deviceLocation.append("->").append(alarm.getFloorName());
                        }
                        if (StringUtils.isNotBlank(alarm.getRoomName())){
                            deviceLocation.append("->").append(alarm.getRoomName());
                        }
                        DepthAlarmDetailExcelVO excelVO = DepthAlarmDetailExcelVO.builder().deviceLocation(deviceLocation.toString())
                                .deviceName(alarm.getDeviceName()).value(alarm.getValue()).createTime(alarm.getCreateTime()).build();
                        dataList.add(excelVO);
                    });
                }
                // 2=按科室统计
                if (Constants.TWO.equals(statisticalMethod)){
                    list.forEach(alarm -> {
                        // 楼栋->楼层->科室->房间
                        StringBuilder deviceLocation = new StringBuilder(alarm.getBuildingName());
                        if (StringUtils.isNotBlank(alarm.getFloorName())){
                            deviceLocation.append("->").append(alarm.getFloorName());
                        }
                        if (StringUtils.isNotBlank(alarm.getDepartmentItemValueName())){
                            deviceLocation.append("->").append(alarm.getDepartmentItemValueName());
                        }
                        if (StringUtils.isNotBlank(alarm.getRoomName())){
                            deviceLocation.append("->").append(alarm.getRoomName());
                        }
                        DepthAlarmDetailExcelVO excelVO = DepthAlarmDetailExcelVO.builder().deviceLocation(deviceLocation.toString())
                                .deviceName(alarm.getDeviceName()).value(alarm.getValue()).createTime(alarm.getCreateTime()).build();
                        dataList.add(excelVO);
                    });
                }
            }
            String fileName = new StringBuffer().append("液氮罐液位深度—报警记录").append(DateUtil.getYmdHms()).append(".xlsx").toString();
            EasyExcelUtil.exportExcelData(response, fileName, dataList, DepthAlarmDetailExcelVO.class);
        }
    }


    /**
     * 温度报警次数
     * @param dto
     * @return
     */
    public Integer temperatureAlarmTotal(LiquidNitrogenAlarmDetailDTO dto){
        AlarmPageDTO alarmPageDTO = AlarmPageDTO.builder().alarmType(Constants.ONE)
                .property(DevicePropertyConstants.liquid_nitrogen_temperature)
                .deviceName(dto.getDeviceName()).paths(dto.getPaths())
                .departmentItemValue(dto.getDepartmentItemValue())
                .code(SpecialityModelCodeEnum.liquid_nitrogen.getCode())
                .build();
        if (StringUtils.isNotBlank(dto.getStartTimeStr())){
            alarmPageDTO.setStartTime(DateUtil.getYmdHms(dto.getStartTimeStr() + " 00:00:00"));
        }
        if (StringUtils.isNotBlank(dto.getEndTimeStr())){
            alarmPageDTO.setEndTime(DateUtil.getYmdHms(dto.getEndTimeStr() + " 23:59:59"));
        }
        return alarmService.total(alarmPageDTO);
    }


    /**
     * 温度报警记录排名(前10)
     * @param dto
     * @return
     */
    public List<AlarmNumRankingVO> ranking(LiquidNitrogenAlarmDetailDTO dto){
        AlarmPageDTO alarmPageDTO = AlarmPageDTO.builder().alarmType(Constants.ONE)
                .property(DevicePropertyConstants.liquid_nitrogen_temperature)
                .deviceName(dto.getDeviceName()).paths(dto.getPaths())
                .departmentItemValue(dto.getDepartmentItemValue())
                .code(SpecialityModelCodeEnum.liquid_nitrogen.getCode())
                .build();
        if (StringUtils.isNotBlank(dto.getStartTimeStr())){
            alarmPageDTO.setStartTime(DateUtil.getYmdHms(dto.getStartTimeStr() + " 00:00:00"));
        }
        if (StringUtils.isNotBlank(dto.getEndTimeStr())){
            alarmPageDTO.setEndTime(DateUtil.getYmdHms(dto.getEndTimeStr() + " 23:59:59"));
        }
        return alarmService.alarmNumRanking(alarmPageDTO, 10);
    }



}
